# -*- coding: utf-8 -*-

import os
import csv
import traceback
import logging
logging.basicConfig(level=logging.DEBUG, format=" [%(asctime)s] - [%(levelname)s] - [%(message)s]")
# logging.basicConfig(filename="runlog.log", level=logging.DEBUG, format=" [%(asctime)s] - [%(levelname)s] - [%(message)s]")
# logging.disable(logging.DEBUG)

class csv_data:
    def __init__(self, path):
        self.path = path

    def return_csv_data(self):
        with open(self.path, "r") as csv_file:
            csv_reader = csv.reader(csv_file)
            csv_data = list(csv_reader)
        return csv_data

    def return_table_data(self):
        with open(self.path, "r") as csv_file:
            csv_reader = csv.reader(csv_file)
            csv_data = list(csv_reader)
            table_data = csv_data[1:]
        return table_data

    def return_table_row(self):
        csv_data = self.return_csv_data()
        if csv_data:
            return len(csv_data) - 1
        else:
            return 0

    def return_table_column(self):
        csv_data = self.return_csv_data()
        if csv_data:
            return len(csv_data[0])
        else:
            return 0

    def return_table_name(self):
        csv_path = self.path
        csv_path = csv_path.split("\\")[-1]
        csv_path = csv_path.split(".")[0:-1]
        table_name = csv_path[-1]
        return table_name

    def return_table_user(self):
        csv_path = self.path
        csv_path = csv_path.split("\\")[-1]
        csv_path = csv_path.split(".")[0:-1]
        table_user = csv_path[0]
        return table_user

    def return_table_field(self):
        idx_table_field = []
        all_table_field = []
        no_idx_table_field = []
        csv_data = self.return_csv_data()
        csv_field = csv_data[0]
        for v_field in csv_field:
            if v_field[0] == "[" and v_field[-1] == "]":
                # v_field = v_field[1:-1]
                idx_table_field.append(v_field)
                all_table_field.append(v_field)
            else:
                no_idx_table_field.append(v_field)
                all_table_field.append(v_field)
        return idx_table_field, no_idx_table_field, all_table_field
